<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入搜索内容" maxlength="20" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery"/>
      <el-select v-model="query.type" clearable placeholder="搜索类型" class="filter-item" style="width: 200px">
        <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      <!-- 新增 -->
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button
          v-permission="['ADMIN','SYSNOTICE_ALL','SYSNOTICE_CREATE']"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add">新增</el-button>
      </div>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;" border>
      <el-table-column type="selection" width="50"/>
      <el-table-column :show-overflow-tooltip="true" prop="name" label="公告名称">
        <template slot-scope="scope">
          <el-link :underline="true" type="primary" @click="viewInfo(scope.row.id)">{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="公告类型" align="center" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.type==1">常见问题</span>
          <span v-if="scope.row.type==2">系统通知</span>
          <span v-if="scope.row.type==3">普通公告</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="公告状态" align="center" width="150">
        <template slot-scope="scope">
            <span v-if="scope.row.status==0" style="color: red;">未发布</span>
            <span v-if="scope.row.status==1" style="color: green;">已发布</span>
          </template>
        </el-table-column>
      <el-table-column prop="createPerson" label="创建人" align="center" width="150"/>
      <el-table-column prop="createtime" label="创建时间" align="center" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['ADMIN','SYSNOTICE_ALL','SYSNOTICE_EDIT','SYSNOTICE_DELETE'])" label="操作" width="150px" align="center">
        <template slot-scope="scope">
          <el-button v-permission="['ADMIN','SYSNOTICE_ALL','SYSNOTICE_EDIT']" size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
          <el-popover
            v-permission="['ADMIN','SYSNOTICE_ALL','SYSNOTICE_DELETE']"
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      layout="total, prev, pager, next, sizes"
      style="margin-top: 8px;"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { del } from '@/api/framework/notice/sysNotice'
import { parseTime } from '@/utils/index'

export default {
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      queryTypeOptions: [
        { key: 'name', display_name: '公告名称' }
      ]
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    checkPermission,
    parseTime,
    beforeInit() {
      this.url = 'api/sysNotice'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) { this.params[type] = value }
      return true
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
      })
    },
    add() {
      this.isAdd = true
      this.$router.push({
        path: '/notice/editor',
        query: {
          isAdd: this.isAdd
        }
      })
    },
    edit(data) {
      this.isAdd = false
      this.$router.push({
        path: '/notice/editor',
        query: {
          isAdd: this.isAdd,
          id: data.id
        }
      })
    },
    viewInfo(id) {
      this.$router.push({
        path: '/notice/noticeInfo',
        query: {
          id: id
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
